package activities;

import handler.Utilities;

import java.util.ArrayList;
import java.util.HashMap;

import model.Artist;
import model.MusicDB;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.lecteurmp3.R;

/**
 * Class of the activity showing the list of Artist
 * @author jean
 *
 */
public class ArtistActivity extends ListActivity {
	private MusicDB musicDb;
	public ArrayList<Artist> artistList = new ArrayList<Artist>();
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		musicDb = new MusicDB(this);
		Utilities util = new Utilities();
		musicDb.open();
		artistList = musicDb.cursorToArtist(musicDb.getAllArtist());
		ArrayList<HashMap<String, String>> musicListView = new ArrayList<HashMap<String,String>>();
		
		for(int i=0;i<artistList.size();i++) {
			Artist a = artistList.get(i);
			musicListView.add(util.ArtistToHashMap(a));
		}
		musicDb.close();
		
		// Add items to ListView
		ListAdapter adapter = new SimpleAdapter(this, musicListView,
												R.layout.list_item_bis,
												new String[] { "artist"},
												new int[] {R.id.titleItem });

		setListAdapter(adapter);
		
		// selecting single ListView item
		ListView lv = getListView();
		// listening to single listitem click
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				getParent().getIntent().putExtra("artistId", artistList.get(position).getId());
				finish();
			}
		});
    }
	
	public void onDestroy(){
		super.onDestroy();
		musicDb.close();
	}

}
